<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="userAccount"
          name="userAccount"
          label="账号"
          placeholder="账号"
          :rules="[{ required: true, message: '请填写账号' }]"
      />
      <van-field
          v-model="userPassword"
          type="password"
          name="userPassword"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</template>

<script setup>
import {ref} from "vue";
import {showFailToast, showSuccessToast} from "vant";
import {useRoute, useRouter} from "vue-router";
import {doLogin} from "../services/api.ts";

const router = useRouter();
const route = useRoute();
const userAccount = ref('')
const userPassword = ref('')

const onSubmit = async () => {
  const loginUser = await doLogin(userAccount.value, userPassword.value);
  if (loginUser) {
    showSuccessToast('登录成功！');
    window.location.href = route.query?.redirect ?? '/'; // 跳转到当时访问页面（没有redirect则跳转到首页）
  } else {
    showFailToast('登录失败！');
  }
}

</script>

<style scoped>

</style>